package com.xys.auth.mapper;


import com.xys.auth.bean.Member;
import com.xys.auth.bean.Orders;
import com.xys.auth.bean.Traveller;
import org.apache.ibatis.annotations.*;

import java.util.List;

public interface OrdersMapper {

    @Select("select * from orders")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "order_num", property = "orderNum"),
            @Result(column = "order_time", property = "orderTime"),
            @Result(column = "people_count", property = "peopleCount"),
            @Result(column = "order_desc", property = "orderDesc"),
            @Result(column = "pay_type", property = "payType"),
            @Result(column = "order_status", property = "orderStatus"),
            @Result(column = "product_id", property = "productId"),
            @Result(column = "member_id", property = "memberId"),

            @Result(column = "product_id", property = "product",
                    one = @One(select = "com.xys.auth.mapper.ProductMapper.findById")
            )
    })
    List<Orders> findAll();

    @Select("select * from orders where id = #{id}")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "order_num", property = "orderNum"),
            @Result(column = "order_time", property = "orderTime"),
            @Result(column = "people_count", property = "peopleCount"),
            @Result(column = "order_desc", property = "orderDesc"),
            @Result(column = "pay_type", property = "payType"),
            @Result(column = "order_status", property = "orderStatus"),
            @Result(column = "product_id", property = "productId"),
            @Result(column = "member_id", property = "memberId"),

            @Result(column = "product_id", property = "product",
                    one = @One(select = "com.xys.auth.mapper.ProductMapper.findById")
            ),
            @Result(column = "member_id", property = "member",
                    one = @One(select = "com.xys.auth.mapper.MemberMapper.findById")
            ),
            @Result(column = "id", property = "travellers",
                    many = @Many(select = "com.xys.auth.mapper.TravellerMapper.findByOrdersId")
            )
    })
    Orders findById(@Param("id") String id);



    @Insert("insert into orders values(#{id},#{orderNum},#{orderTime},#{peopleCount},#{orderDesc},#{payType},#{orderStatus},#{productId},#{memberId})")
    int  insert(Orders  orders);



    @Update("update orders set product_id=#{productId} where id=#{id}")
    void updateProduct(@Param("id")String  id, @Param("productId")String productId);


    @Insert("insert into traveller values(#{id},#{name},#{sex},#{phoneNum},#{credentialsType},#{credentialsNum},#{travellerType})")
    int insertTraveller(Traveller traveller);


    @Insert("insert into order_traveller values(#{ordersId},#{travellerId})")
    void insertTravellerAndOrders( @Param("ordersId")String ordersId, @Param("travellerId") String travellerId);


    @Update("update orders set member_id=#{memberId} where id=#{id}")
    void updateMember(@Param("id")String  id, @Param("memberId")String memberId);


    @Insert("insert into member values(#{id},#{name},#{nickname},#{phoneNum},#{email})")
    int insertMember(Member member);
    @Update("update orders set order_status=1  where id=#{id}")
    void update(@Param("id") String id);

    @Delete("delete from orders where id = #{id}")
    void delete(@Param("id") String id);

    @Delete("delete from order_traveller where order_id=#{orderId}")
    void deleteOrderTravellerById(@Param("orderId") String id);
}
